//
//  LLNetworkServiceManager.m
//  LLFoundation
//
//  Created by my_liu on 2016/11/14.
//  Copyright © 2016年. All rights reserved.
//

#import "LLNetworkServiceManager.h"
#import "NSArray+LLHelper.h"
#import "NSDictionary+LLHelper.h"
#import "LLLoggerUitilsMacrocDefine.h"

@interface LLNetworkServiceManager ()
{
    NSDictionary *_defaultHeader;
}
@end

@implementation LLNetworkServiceManager

+ (instancetype)share
{
    static LLNetworkServiceManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[LLNetworkServiceManager alloc] init];
    });
    return manager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _defaultHeader = [NSDictionary dictionary];
    }
    return self;
}

- (void)setDefaultHeader:(NSDictionary *)defaultHeader
{
    if (defaultHeader) {
        _defaultHeader = defaultHeader;
    } else {
        _defaultHeader = [NSDictionary dictionary];
    }
}

- (NSDictionary *)getDefaultHeader
{
    return _defaultHeader;
}

- (NSUInteger)requestGetUrl:(NSString *)url successBlock:(LHttpSuccessBlock)successBlock failBlock:(LHttpFailBlock)failBlock userInfo:(NSDictionary *)userInfo
{
    return [self requestGetUrl:url header:nil parameters:nil successBlock:successBlock failBlock:failBlock userInfo:userInfo];
}

- (NSUInteger)requestGetUrl:(NSString *)url header:(NSMutableDictionary *)header successBlock:(LHttpSuccessBlock)successBlock failBlock:(LHttpFailBlock)failBlock userInfo:(NSDictionary *)userInfo
{
    return [self requestGetUrl:url header:header parameters:nil successBlock:successBlock failBlock:failBlock userInfo:userInfo];
}

- (NSUInteger)requestGetUrl:(NSString *)url parameters:(id)parameters successBlock:(LHttpSuccessBlock)successBlock failBlock:(LHttpFailBlock)failBlock userInfo:(NSDictionary *)userInfo
{
    return [self requestGetUrl:url header:nil parameters:parameters successBlock:successBlock failBlock:failBlock userInfo:userInfo];
}

- (NSUInteger)requestGetUrl:(NSString *)url header:(NSMutableDictionary *)header parameters:(id)parameters successBlock:(LHttpSuccessBlock)successBlock failBlock:(LHttpFailBlock)failBlock userInfo:(NSDictionary *)userInfo
{
    LLLog(@"完整的Url %@",url);
    NSDictionary *defaultHeader = [self getDefaultHeader];
    if (!header) {
        header = [NSMutableDictionary dictionary];
    }
    for (NSString *key in [defaultHeader allKeys]) {
        if ([[header allKeys] containsObject:key]) {
            continue;
        }
        [header setObject:[defaultHeader objectForKey:key] forKey:key];
    }
    LLNWHTTPDetails *httpDetails = [[LLNWHTTPDetails alloc] init];
    httpDetails.requestMethod = kLLNWGetMethod;
    httpDetails.requestHost = url;
    httpDetails.appendHeader = header;
    httpDetails.requestKey = [NSString stringWithFormat:@"NetCancel%ld",(unsigned long)httpDetails.hash];
    httpDetails.parameters = parameters;
    [LLNWHTTPLink requestSource:self withInfo:httpDetails success:^(LLNWHTTPResponse *response) {
        if (successBlock) {
            successBlock(response.responseData,userInfo);
        }
    } failure:^(LLNWHTTPResponse *response) {
        if (failBlock) {
            failBlock(response.error,userInfo);
        }
    }];
    NSUInteger hashNumber = httpDetails.hash;
    return hashNumber;
}

- (NSUInteger)requestPostUrl:(NSString *)url parameters:(id)parameters successBlock:(LHttpSuccessBlock)successBlock failBlock:(LHttpFailBlock)failBlock userInfo:(NSDictionary *)userInfo
{
    return [self requestPostUrl:url parameters:parameters header:nil body:nil successBlock:successBlock failBlock:failBlock userInfo:userInfo];
}

- (NSUInteger)requestPostUrl:(NSString *)url parameters:(id)parameters header:(NSMutableDictionary *)header successBlock:(LHttpSuccessBlock)successBlock failBlock:(LHttpFailBlock)failBlock userInfo:(NSDictionary *)userInfo
{
    return [self requestPostUrl:url parameters:parameters header:header body:nil successBlock:successBlock failBlock:failBlock userInfo:userInfo];
}
- (NSUInteger)requestPostUrl:(NSString *)url parameters:(id)parameters body:(NSString *)body successBlock:(LHttpSuccessBlock)successBlock failBlock:(LHttpFailBlock)failBlock userInfo:(NSDictionary *)userInfo
{
    return [self requestPostUrl:url parameters:parameters header:nil body:body successBlock:successBlock failBlock:failBlock userInfo:userInfo];
}

- (NSUInteger)requestPostUrl:(NSString *)url header:(NSMutableDictionary *)header body:(NSString *)body successBlock:(LHttpSuccessBlock)successBlock failBlock:(LHttpFailBlock)failBlock userInfo:(NSDictionary *)userInfo
{
    return [self requestPostUrl:url parameters:nil header:header body:body successBlock:successBlock failBlock:failBlock userInfo:userInfo];
}

- (NSUInteger)requestPostUrl:(NSString *)url parameters:(id)parameters header:(NSMutableDictionary *)header body:(NSString *)body successBlock:(LHttpSuccessBlock)successBlock failBlock:(LHttpFailBlock)failBlock userInfo:(NSDictionary *)userInfo;
{
    LLLog(@"完整的Url %@",url);
    NSDictionary *defaultHeader = [self getDefaultHeader];
    if (!header) {
        header = [NSMutableDictionary dictionary];
    }
    for (NSString *key in [defaultHeader allKeys]) {
        
        if ([[header allKeys] containsObject:key]) {
            continue;
        }
        
        [header setObject:[defaultHeader objectForKey:key] forKey:key];
    }
    
    LLNWHTTPDetails *httpDetails = [[LLNWHTTPDetails alloc] init];
    httpDetails.requestMethod = kLLNWPostMethod;
    httpDetails.requestHost = url;
    httpDetails.appendHeader = header;
    httpDetails.requestKey = [NSString stringWithFormat:@"NetCancel%ld",(unsigned long)httpDetails.hash];
    httpDetails.requestBody = [body dataUsingEncoding:NSUTF8StringEncoding];
    httpDetails.parameters = parameters;
    [LLNWHTTPLink requestSource:self withInfo:httpDetails success:^(LLNWHTTPResponse *response) {
        if (successBlock) {
            successBlock(response.responseData,userInfo);
        }
    } failure:^(LLNWHTTPResponse *response) {
        if (failBlock) {
            failBlock(response.error,userInfo);
        }
    }];
    NSUInteger hashNumber =  httpDetails.hash;
    return hashNumber;
}

- (void)cancelRequestByReqId:(NSUInteger)reqId
{
    [LLNWHTTPLink cancelRequestWithRequestKey:[NSString stringWithFormat:@"NetCancel%ld",(long)reqId]];
}

- (void)cancelAllRequest
{
    //    [[CUHttpHelper share] cancelAllRequest];
}
@end
